package cn.iocoder.yudao.module.blog.controller.app.report;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.module.blog.controller.admin.report.vo.BlogReportCreateReqVO;
import cn.iocoder.yudao.module.blog.service.report.BlogReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "用户 APP - 上报")
@RestController
@RequestMapping("/blog/report")
@Validated
public class AppBlogReportController {

    @Resource
    private BlogReportService reportService;

    @Autowired
    HttpServletRequest request;

    @PostMapping("/create")
    @Operation(summary = "创建上报统计")
    public CommonResult<Long> createReport(@Valid @RequestBody BlogReportCreateReqVO createReqVO) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        createReqVO.setIp(HttpUtils.getIpAddress(request));

        return success(reportService.createReport(createReqVO));
    }
}
